//最新的创建AI函数
//type	0突击队 1标准 2守卫
//pos 位置（自动规避离玩家太近）
//dis 距离
//num 数量
//level 等级 突击队填1代表一开始就移动
//
params["_gType","_type", "_pos", ["_dis", 0], ["_num", 0], ["_level", 0], ["_water", false]];

if(_gType == 0) then{_num = 1;};

if(_num <= 0)exitWith{if(Param_Debug == 1) then{systemChat "createGNew创建数量太小"}};


//位置计算，小于0代表重建
if(_dis >= 0) then
{
	if(_gType == 0)then{
		if(_dis == 0) then{
			_pos = [_pos] call fn_getPosNearLocal;
		}
		else{
			_pos = [_pos, _dis, 250] call fn_getPosNearLand;
		};
	}
	else{
		if(_gType == 1)then{
			if(_water) then{
				_pos = _pos getPos[_dis * (Param_EnemySpawnDis + 0.3), random 360];
			}else{
				_pos = [_pos, _dis * (Param_EnemySpawnDis + 0.3), 50] call fn_getPosNearLand;
			};
		}
		else{
			_pos = _pos;
		}
	};
};
//距离检查
if(!(isNull ([_pos, 300] call fn_getPlayerNear)))exitWith{if(Param_Debug == 1) then{systemChat "createGNew离玩家太近"}};

if((_pos distance2D (getMarkerPos "mk_players")) < 400)exitWith{if(Param_Debug == 1) then{systemChat "createGNew离出生点太近"}};

//突击队特殊检查
if(_gType == 0
&& _level == 0
&& {(_pos distance2D as_start_pos) < 2000})exitWith{if(Param_Debug == 1) then{systemChat "createGNew离西安太近"}};

as_gn_num = as_gn_num + _num * 10;
publicVariable "as_gn_num";
/*
private _unit = as_group_gn createUnit ["C_Soldier_VR_F", _pos, [], 10, "NONE"];
_unit allowDamage false;
_unit disableAI "ALL";
*/
private _marker = "";
if(Param_Debug == 1) then
{
	//if(_gType == 2) exitWith{};
	//_marker = createMarker [format["gn_%1", as_gn_num], _pos];

	
	_marker setMarkerShape "ICON";
	_marker setMarkerType "hd_dot";
	
	switch (_gType) do
	{
		case 0:{_marker setMarkerColor "ColorRed";_marker setMarkerText format["t0-%1-%2", _num, _level]};
		case 1:{_marker setMarkerColor "ColorBlue";_marker setMarkerText format["t1-%1-%2", _num, _level]};
		case 2:{_marker setMarkerColor "ColorGreen";_marker setMarkerText format["t2-%1-%2", _num, _level]};
		default {_marker setMarkerText "t_error"};
	};
};



//目标
private _target = objNull;
private _defendExit = false;

while{true} do{
	private _pos_target = [-10000,-10000];//目标位置
	//支援距离小于一定值，就开始移动
	{
		private _pos2 = _x select 0;
		private _k = _x select 1;
		if(_pos distance2D _pos2 < Param_EnemySupportDis * _k)exitWith{
			_pos_target = _pos2;
			if(Param_Debug == 1) then
			{
				private _marker = createMarker [format["marker_wp_%1", as_waypoint_cur], _pos_target];
				_marker setMarkerShape "ICON";
				_marker setMarkerColor "ColorRed";
				_marker setMarkerType "mil_arrow";
				_marker setMarkerText "支援";
				as_waypoint_cur = as_waypoint_cur + 1;
			};
		};
	}forEach as_gn_target;
	
	
	//进入玩家范围
	private _player = [_pos, Param_SimulationDistance * 1.25] call fn_getPlayerNear;
	if(!isNull _player)exitWith{
		//小于距离才锁定
		if(_player distance2D _pos_target < 500)then{
			_target = player;
			if(Param_Debug == 1) then{systemChat "锁定玩家"};
		};
	};
	
	Sleep 60;
	
	//开始防守了，则移出逻辑
	if(!isNil "as_defend_state") exitWith{
		_defendExit = true;
		if(Param_Debug == 1) then{deleteMarker _marker};
	};
	
	
	//计算移动（帧率要求）
	if(_pos_target select 0 > 0
		&& {call fn_canSpawnAI})then{
		_pos = _pos getPos[140 / Param_EnemySupportTime, _pos getDir _pos_target];
	};

	if(Param_Debug == 1) then{_marker setMarkerPos _pos};
};

if(_defendExit) exitWith{};

private _groupRet = grpNull;

if(_gType == 0) then{
	_groupRet = [_type, _pos, ((!isNull _target) || {_level == 1})] call fn_createG;
};
if(_gType == 1) then{
	_groupRet = [_pos, _num, _level, 0, _type, true] call fn_createGroup;
	[_groupRet] spawn fn_controlGroup;
	
	if(!isNull _target) then{
		[_groupRet, _target] call fn_addWaypoint;
	};
};
if(_gType == 2) then{
	
	private _l = asu_i_c;
	if(_level == 0) then
	{
		_l = asu_i_c;
	};
	if(_level == 1) then
	{
		_l = asu_i_g;
	};
	if(_level == 2) then
	{
		_l = asu_i;
	};
	if(_level == 3) then
	{
		_l = asu_b;
	};
	_groupRet = [_pos, _num, _l] call fn_createGroup2;
	
	if(!isNull _target) then{
		[_groupRet, _target] call fn_addWaypoint;
	};
};


if(Param_Debug == 1) then
{
	systemChat "createGNew创建了一队";
	deleteMarker _marker;
};

if(isNull _groupRet
 || {count units _groupRet == 0}) exitWith{if(Param_Debug == 1) then{systemChat "createGNew创建失败，或数量为0"};_groupRet};

//重建
[_groupRet, _gType, _type, _level, _water, _num / (count units _groupRet)] spawn{
	params["_group", "_gType", "_type", "_level", "_water", "_numK"];
	while{true} do{
		Sleep 30;
		if(isNull _group
		|| {count units _group == 0})exitWith{};
		private _unit = leader _group;
		
		private _target = [_unit, Param_SimulationDistance * 1.5] call fn_getPlayerDis;

		if(isNull _target) exitWith{
			//数量按比例传递
			[_gType, _type, getPos _unit, -100, (count (units _group)) * _numK, _level, _water] spawn fn_createGN;
			
			if(Param_Debug == 1) then{systemChat "createGNew重建了一组"};
			{
				deleteVehicle vehicle _x;
				deleteVehicle _x;
			}forEach units _group;
			deleteGroup _group;
		};
	};
};

//只队长开启IR
(leader _groupRet) enableIRLasers true;

//返回值有待商榷
_groupRet